﻿using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.SystemManage;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Application.SystemManage
{
    public class AreaApp
    {
        private IAreaRepository service = new AreaRepository();

        public List<AreaEntity> GetProviceJson()
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers ==1);
            return service.FindList(expression);
        }
        public List<AreaEntity> GetCityJson(string ProId)
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers == 2);
            expression = expression.And(t => t.F_ParentId.Equals(ProId));
            return service.FindList(expression);
        }
        public List<AreaEntity> GetAreaJson(string City)
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers == 3);
            expression = expression.And(t => t.F_ParentId.Equals(City));
            return service.FindList(expression);
        }
        public List<AreaEntity> GetProviceTJson()
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers == 1);
            List<AreaEntity> entitys = service.FindList(expression);
            foreach (AreaEntity a in entitys)
            {
                if (a.IsUsed == 1)
                    a.F_FullName = a.F_FullName + "(已代理)";
            }
            return entitys;
            //return service.FindList(expression);
        }
        public List<AreaEntity> GetCityTJson(string ProId)
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers == 2);
            expression = expression.And(t => t.F_ParentId.Equals(ProId));
            List<AreaEntity> entitys = service.FindList(expression);
            foreach (AreaEntity a in entitys)
            {
                if (a.IsUsed == 1)
                    a.F_FullName = a.F_FullName + "(已代理)";
            }
            return entitys;
            //return service.FindList(expression);
        }
        public List<AreaEntity> GetAreaTJson(string City)
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_Layers == 3);
            expression = expression.And(t => t.F_ParentId.Equals(City));
            List<AreaEntity> entitys = service.FindList(expression);
            foreach (AreaEntity a in entitys)
            {
                if (a.IsUsed == 1)
                    a.F_FullName = a.F_FullName + "(已代理)";
            }
            return entitys;
            //return service.FindList(expression);
        }
        public List<AreaEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<AreaEntity> GetAgList(string keyValue)
        {
            var expression = ExtLinq.True<AreaEntity>();
            expression = expression.And(t => t.F_FullName.Contains(keyValue));
            expression = expression.Or(t => t.F_EnCode.Contains(keyValue));
            expression = expression.Or(t => t.F_ParentId.Contains(keyValue));
            return service.GetAgList(expression);
        }
        public AreaEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                service.Delete(t => t.F_Id == keyValue);
            }
        }
        public void SubmitForm(AreaEntity areaEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                areaEntity.Modify(keyValue);
                service.Update(areaEntity);
            }
            else
            {
                areaEntity.Create();
                service.Insert(areaEntity);
            }
        }
    }
}
